package com.yx.gsyd.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yx.gsyd.comm.ResultEnum;
import com.yx.gsyd.entity.SysJydz;
import com.yx.gsyd.entity.SysManager;
import com.yx.gsyd.entity.excel.SysJydzExcel;
import com.yx.gsyd.exception.MyException;
import com.yx.gsyd.qpage.QPage;
import com.yx.gsyd.service.ISysJydzService;
import com.yx.gsyd.util.ExcelUtil;
import com.yx.gsyd.util.ResultView;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-29
 */
@RestController
@RequestMapping("/api/admin/sys-jydz")
public class SysJydzController {

    @Autowired
    private ISysJydzService service;

    @Resource
    private ResourceLoader resourceLoader;

    /**
     * 列表分页
     * @param obj
     * @return
     */
    @GetMapping("/getPageList")
    public ResultView getPageList(QPage qPage, SysJydz obj) {
        return this.service.getPageList(qPage,obj);
    }

    /**
     * 获取分配的业务员
     * @return
     */
    @GetMapping("/getSelect")
    public ResultView getSelect() {
        QueryWrapper<SysJydz> queryWrapper=new QueryWrapper<>();
        List<SysJydz> sysJydzList=this.service.list(null);
        return ResultView.ok(sysJydzList);
    }

    /**
     * 根据ID获取当前数据
     * @return
     */
    @GetMapping("/getById")
    public ResultView getById(@NotNull(message = "ID不能为空") String jyId) {
        return ResultView.ok(this.service.getById(jyId));
    }

    /**
     * 新增
     * @param obj
     * @return
     */
    @PostMapping("/add")
    public ResultView add(@Validated SysJydz obj) {
        return this.service.add(obj);
    }

    /**
     * 删除
     * @return
     */
    @PostMapping("/del")
    public ResultView del(@NotNull(message = "ID不能为空") String jyId) {
        return this.service.del(jyId);
    }


    /**
     * 修改
     * @param obj
     * @return
     */
    @PostMapping("/up")
    public ResultView update(@Validated SysJydz obj) {
        return this.service.up(obj);
    }



    /**
     * 下载模板
     * @return
     */
    @RequestMapping("/downLoadFile")
    public void export(HttpServletResponse response) throws IOException {
        String path = "excel/经营地址导入模板.xlsx";
        InputStream inputStream = null;
        ServletOutputStream servletOutputStream = null;
        try {
            String filename = "经营地址导入模板.xlsx";
            org.springframework.core.io.Resource resource = resourceLoader.getResource("classpath:"+path);
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.addHeader("charset", "utf-8");
            response.addHeader("Pragma", "no-cache");
            String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
            response.setHeader("Content-Disposition", "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);
            inputStream = resource.getInputStream();
            servletOutputStream = response.getOutputStream();
            IOUtils.copy(inputStream, servletOutputStream);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (servletOutputStream != null) {
                    servletOutputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
                // 召唤jvm的垃圾回收器
                System.gc();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 导入演示Excel
     * @return
     */
    @RequestMapping("/importExcel")
    public ResultView importExcel(@RequestParam("uploadfile") MultipartFile uploadfile) throws IOException {
        if (uploadfile.isEmpty()) {
            throw new MyException(ResultEnum.CODE_2,"抱歉，系統检查文件没有内容，或者为损坏状态，上传失败!!!");
        }
        List<SysJydzExcel> ulist=  ExcelUtil.syncReadModel(uploadfile.getInputStream(), SysJydzExcel.class,0,1);
        if (ulist == null || ulist.size() <= 0) {
            throw new MyException(ResultEnum.CODE_2,"此文件没有解析可导入的数据，导入失败");
        }
        return this.service.importExcel(ulist);
    }



}
